library(dplyr); library(broom)
library(ggplot2); library(estimatr)
library(texreg); library(readr)
library(grid); library(forcats)

placebo <- read_csv("placebo_experiment.csv")

placebo <- placebo %>%
  mutate(
    political = as.factor(political),
    political = relevel(political, "Control"),
    positive = as.factor(positive),
    positive = relevel(positive, "Control")
  )

# Table 3
main.model <- lm_robust(outcome ~ conditions + wave + study, data = placebo,
                 clusters = id)

texreg(main.model, ci.force = TRUE,
       omit.coef = "wave|Intercept|study")

# Table 5
positive.model <- lm_robust(outcome ~ positive + wave + study, 
                            data = placebo,
                            clusters = id,
                            subset =  conditions == "Placebo" | conditions == "Control")

texreg(positive.model, ci.force = TRUE,
       omit.coef = "wave|Intercept|study")

# Table 6
political.model <- lm_robust(outcome ~ political + wave + study, data = placebo, 
                     subset = conditions == "Placebo" | conditions == "Control",
          clusters = id)

texreg(political.model, ci.force = TRUE,
       omit.coef = c("wave|Intercept|study"))

# Figure 1
placebo %>%
  group_by(study) %>%
  do(tidy(lm_robust(outcome ~ political + wave, data = .,
             clusters = id))) %>%
  mutate(condition = plyr::mapvalues(term, c("(Intercept)",
                                             "politicalPolitical", 
                                             "politicalFree Speech", 
                                             "politicalApolitical", 
                                             "politicalSafety", 
                                             "politicalStudent Loans"), 
                                     c("Control", 
                                       "Political Placebo", 
                                       "Free Speech", 
                                       "Apolitical Placebo", 
                                       "Public Safety", 
                                       "Student Loans")),
         estimate = round(estimate, 2)) %>%
  filter(condition != "wave" & condition != "Control") %>%
  filter(study == "Student Loans Experiment") %>%
  ggplot(aes(x = forcats::fct_reorder(condition, estimate), y = estimate)) +
  geom_linerange(aes(ymin = estimate - 1.405*std.error,
                      ymax = estimate + 1.405*std.error), size = 1) +
  geom_linerange(aes(ymin = estimate - 1.96*std.error,
                      ymax = estimate + 1.96*std.error), size = .35) +
  geom_point(color = "gray90", size = 11) +
  geom_point(color = "white", size = 9) +
  geom_text(aes(label = estimate), size = 3) +
  coord_flip() +
  geom_hline(yintercept = 0, alpha = .5) +
  scale_color_grey() +
  theme_minimal() +
  ylim(-.35, .35) +
  labs(x = "", y = "Effect Estimate (Control Group SDs)", title = "") +
  theme(text=element_text(size=14,  family="Palatino"),
        legend.position = "bottom",
        panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  ggtitle("Consequences of Placebo Selection")

ggsave("figure1.pdf")

# Figure 2
placebo %>%
  group_by(study) %>%
  do(tidy(lm_robust(duration ~ conditions, data = .,
                    clusters = id))) %>%
  mutate(condition = as.factor(plyr::mapvalues(term, c("(Intercept)", 
                                             "conditionsFree Speech", 
                                             "conditionsSafety", 
                                             "conditionsStudent Loans",
                                             "conditionsPlacebo"), 
                                     c("Control", 
                                       "Free Speech",  
                                       "Public Safety", 
                                       "Student Loans",
                                       "Placebo")))) %>%
  mutate(condition = fct_relevel(condition, "Placebo", after = 0)) %>%
  filter(condition != "Control") %>%
  mutate(estimate = round(estimate, 1)) %>%
  ggplot(aes(x = condition,
             y = estimate)) +
  geom_hline(yintercept = 0, alpha = .5) +
  geom_linerange(aes(ymin = estimate - 1.405*std.error,
                     ymax = estimate + 1.405*std.error), size = 1) +
  geom_linerange(aes(ymin = estimate - 1.96*std.error,
                     ymax = estimate + 1.96*std.error), size = .35) +
  geom_point(color = "gray90", size = 11) +
  geom_point(color = "white", size = 9) +
  geom_text(aes(label = estimate), size = 3) +
  facet_grid(study ~ ., scales = "free") +
  coord_flip() +
  labs(x = "", y = "Effect Estimate (Duration)", 
       title = "Survey Duration by Experimental Condition") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme_minimal() +
  theme(text=element_text(size=14,  family="Palatino"),
        legend.position = "bottom",
        panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())

ggsave("figure2.pdf")

###################
#### APPENDIX #####
###################

# Figure 3 in Appendix
placebo %>% 
  group_by(conditions) %>% 
  filter(conditions != "Control") %>%
  dplyr::select(genuine) %>% 
  dplyr::summarise(avg = mean(genuine, na.rm = T), n = n(), 
         sd = sd(genuine, na.rm = T), se = sd/sqrt(n)) %>%
  mutate(Condition = c("Student Loans",
                        "Placebo",
                        "Free Speech",
                        "Public Safety")) %>%
  mutate(Condition = fct_relevel(Condition, "Placebo", after = 0)) %>%
  ggplot(aes(x = Condition, y  = avg)) +
  geom_point(size = 1) +
  coord_flip() + 
  geom_errorbar(aes(ymin = avg - 1.96*se, ymax = avg + 1.96*se),
                width = 0) +
  theme_minimal() +
  labs(x = "") + 
  scale_y_discrete(name ="How confident are you that the article you read \n is a genuine news story, describing actual events?", 
                   limits=c("Not at all \n confident",
                            "Only slightly \n confident",
                            "Somewhat \n confident",
                            "Moderately \n confident",
                            "Very \n confident"),
                   drop = FALSE) +
  theme(text=element_text(size=14,  family="Palatino"),
        legend.position = "bottom",
        panel.border = element_blank(), 
        panel.grid.minor = element_blank()) +
  ggtitle("Plausibility of Treatment and Placebo Vignettes") -> p

placebo %>% 
  group_by(conditions) %>% 
  filter(conditions != "Control") %>%
  dplyr::select(computer) %>% 
  dplyr::summarise(avg = mean(computer, na.rm = T), n = n(), 
                   sd = sd(computer, na.rm = T), se = sd/sqrt(n)) %>%
  mutate(Condition = c("Student Loans",
                       "Placebo",
                       "Free Speech",
                       "Public Safety")) %>%
  mutate(Condition = fct_relevel(Condition, "Placebo", after = 0)) %>%
  ggplot(aes(x = Condition, y  = avg)) +
  geom_point(size = 1) +
  coord_flip() + 
  geom_errorbar(aes(ymin = avg - 1.96*se, ymax = avg + 1.96*se),
                width = 0) +
  theme_minimal() +
  labs(x = "") +
  scale_y_discrete(name ="Do you think the article you just \n read was written by a human or a computer?", 
                   limits=c("Definitely \n Human",
                            "Probably \n Human",
                            "Either Human \n or Computer",
                            "Probably \n Computer",
                            "Definitely \n Computer"),
                   drop = FALSE) +
  theme(text=element_text(size=14,  family="Palatino"),
        legend.position = "bottom",
        panel.border = element_blank(), 
        panel.grid.minor = element_blank())  -> p2

grid.newpage()
grid.draw(rbind(ggplotGrob(p), ggplotGrob(p2), size = "last"))

ggsave("figure3.pdf")

# Figure 4 in Appendix
placebo %>%
  group_by(study) %>%
  mutate(conditions = relevel(as.factor(conditions), "Placebo")) %>%
  do(tidy(lm_robust(outcome ~ conditions + wave, data = .,
                    clusters = id, subset = conditions != "Control"))) -> placebo.fx

placebo %>%
  group_by(study) %>%
  mutate(conditions = relevel(as.factor(conditions), "Control")) %>%
  do(tidy(lm_robust(outcome ~ conditions + wave, data = .,
                    clusters = id, subset = conditions != "Placebo"))) -> control.fx

placebo.fx %>%
  bind_rows(control.fx) %>%
  filter(term != "wave" & term != "(Intercept)") %>%
  mutate(term = as.factor(plyr::mapvalues(term, c("(Intercept)", 
                                                       "conditionsFree Speech", 
                                                       "conditionsSafety", 
                                                       "conditionsStudent Loans"), 
                                               c("Control", 
                                                 "Free Speech",  
                                                 "Public Safety", 
                                                 "Student Loans"))),
         estimate = round(estimate, 2)) %>%
  bind_cols(baseline = c(rep("Relative to Placebo", 3), 
                         rep("Relative to Pure Control", 3))) %>%
  ggplot(aes(x = term, y = estimate, color = baseline, group = baseline)) +
  geom_linerange(aes(ymin = estimate - 1.405*std.error,
                     ymax = estimate + 1.405*std.error), size = 1,
                 position = position_dodge(.65)) +
  geom_linerange(aes(ymin = estimate - 1.96*std.error,
                     ymax = estimate + 1.96*std.error), size = .35,
                 position = position_dodge(.65)) +
  geom_point(color = "black", size = 8, position = position_dodge(.65)) +
  geom_point(color = "white", size = 7.8, position = position_dodge(.65)) +
  coord_flip() +
  geom_text(aes(label = estimate), size = 3,
            position = position_dodge(.65), color = "black") +
  theme_minimal() +
  scale_color_grey() +
  geom_hline(yintercept = 0) +
  labs(color = "", x = "", y = "Effect Estimate") +
  theme(text=element_text(size=14,  family="Palatino"),
        legend.position = "bottom",
        panel.border = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  ggtitle("Use of Pure Control vs. Placebo as Baseline")
  
ggsave("figure4.pdf")
